/*
 * Created on 12 sept. 2008
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package logogui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Hashtable;
import java.util.Random;

import javax.imageio.ImageIO;

public class Traceur {
	private static Traceur instance;
	private Graphics2D g2d;
	private double initx = 300, inity = 300; // position initiale
	private double posx = initx, posy = inity; // position courante
	private int angle = 90;
	private double teta;
	private boolean dessiner = true;
	private boolean affichageTortue = true;
	private int cap = 0;
	Hashtable<String, Double> h;
	Random r;
	Date date;

	private BufferedImage image;
	private int largeur;
	private int hauteur;

	public Traceur() {
		setTeta();
		h = new Hashtable<String, Double>();
		r = new Random();
		date = new Date();
		long seed = date.getTime();
		r.setSeed(seed);
	}

	public static Traceur getInstance() {
		if (instance == null)
			instance = new Traceur();
		return instance;
	}

	public void setGraphics(Graphics g) {
		g2d = (Graphics2D) g;
	}

	private int toInt(double a) {
		return (int) Math.round(a);
	}

	public double sinus(double a) {
		return Math.sin(a);
	}
	
	public void avance(double r) {
		System.out.println("Avancer");
		double a = posx + r * Math.cos(teta);
		double b = posy - r * Math.sin(teta);
		int x1 = toInt(posx);
		int y1 = toInt(posy);
		int x2 = toInt(a);
		int y2 = toInt(b);
		if (dessiner)
			g2d.drawLine(x1, y1, x2, y2);
		posx = a;
		posy = b;
	}

	public void recule(double r) {
		System.out.println("Reculer");
		double a = posx - r * Math.cos(teta);
		double b = posy + r * Math.sin(teta);
		int x1 = toInt(posx);
		int y1 = toInt(posy);
		int x2 = toInt(a);
		int y2 = toInt(b);
		if (dessiner)
			g2d.drawLine(x1, y1, x2, y2);
		posx = a;
		posy = b;
	}

	public void fpos(double m, double n) {
		System.out.println("Fixer position");
		int x1 = toInt(posx);
		int y1 = toInt(posy);
		int x2 = toInt(m);
		int y2 = toInt(n);
		if (dessiner)
			g2d.drawLine(x1, y1, x2, y2);
		posx = m;
		posy = n;
	}

	public void td(double r) {
		System.out.println("Tourner a droite");
		angle = (angle - toInt(r)) % 360;
		setTeta();
		cap = (cap + toInt(r)) % 360;
	}

	public void tg(double r) {
		System.out.println("Tourner a gauche");
		angle = (angle + toInt(r)) % 360;
		setTeta();
		cap = (cap - toInt(r)) % 360;
	}

	public void fcap(double r) {
		System.out.println("Fixer cap");
		teta = Math.toRadians(r);
		cap = toInt(r) % 360;
		if (r >= 0 && r <= 90) {
			angle = (90 - cap) % 360;
		} else if (r > 90 && r <= 360) {
			angle = (450 - cap) % 360;
		}
		setTeta();
	}

	public int getCap() {
		return cap;
	}

	public void fcc(double r) {
		System.out.println("Fixer couleur crayon");
		switch ((int) r % 8) {
		case 0:
			g2d.setColor(Color.black);
			break;
		case 1:
			g2d.setColor(Color.red);
			break;
		case 2:
			g2d.setColor(Color.green);
			break;
		case 3:
			g2d.setColor(Color.yellow);
			break;
		case 4:
			g2d.setColor(Color.blue);
			break;
		case 5:
			g2d.setColor(Color.magenta);
			break;
		case 6:
			g2d.setColor(Color.cyan);
			break;
		case 7:
			g2d.setColor(Color.white);
			break;
		default:
			g2d.setColor(Color.black);
			break;
		}
	}

	public void lc() {
		System.out.println("Lever crayon");
		dessiner = false;
	}

	public void bc() {
		System.out.println("Baisser crayon");
		dessiner = true;
	}

	public void ve() {
		System.out.println("Effacer ecran");
		g2d.clearRect(0, 0, 1000, 1000);
	}

	public void mt() {
		System.out.println("Montrer tortue");
		affichageTortue = true;
	}

	public void ct() {
		System.out.println("Cacher tortue");
		affichageTortue = false;
	}

	private void setTeta() {
		teta = Math.toRadians(angle);
	}

	public double random(double n) {
		int nb = new Float(Math.random() * n).intValue();
		return nb;
	}

	public void affect(String name, double value) {
		h.put(name, value);
	}

	public double getMap(String name) {
		return h.get(name);
	}

	public void dessinerTortue() {
		try {
			image = ImageIO.read(new File("rsc\\tortue.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		largeur = image.getWidth();
		hauteur = image.getHeight();
		AffineTransform tx = new AffineTransform();
		tx.rotate(Math.toRadians(cap), largeur / 2, hauteur / 2);
		AffineTransformOp op = new AffineTransformOp(tx,
				AffineTransformOp.TYPE_BILINEAR);
		image = op.filter(image, null);
		if (affichageTortue) {
			g2d.drawImage(image, (int) (posx - largeur / 2),
					(int) (posy - hauteur / 2), null);
		}
	}
}
